package com.hxj.springboot01.controller;
import com.hxj.springboot01.mapper.PersonMapper;
import com.hxj.springboot01.pojo.Person;
import com.hxj.springboot01.util.WebNameCrawler;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

@RestController
public class PersonController {

    @Autowired
    private PersonMapper personMapper;

    @Tag(name = "霍兴建的测试接口", description = "霍兴建的描述测试接口。。。")
    @Operation(summary = "hello接口", description = "hello接口描述" ,hidden = false)
    @GetMapping("/queryPersons")
    public String queryPersons(){
        List<Person> people = personMapper.queryPersons();
        return people.toString();
    }

    @Tag(name = "添加用户信息", description = "爬取数据重组姓名添加道数据库用户表数据接口。")
    @Operation(summary = "添加", description = "添加用户" ,hidden = false)
    @GetMapping("/insertPersons")
    public Integer insertPersons() throws IOException {
        ArrayList<String> str = WebNameCrawler.getNames();
        Iterator<String> it = str.iterator();
        List<Person> list = new ArrayList<>();
        String[] provinces = new String[] {
                "北京市", "天津市", "上海市", "重庆市",
                "河北省", "山西省", "陕西省", "吉林省", "黑龙江省",
                "江苏省", "浙江省", "安徽省", "福建省", "江西省",
                "山东省", "河南省", "湖北省", "湖南省", "广东省",
                "广西壮族自治区", "海南省", "四川省", "贵州省", "云南省",
                "西藏自治区", "陕西省", "甘肃省", "甘南省", "青海省",
                "宁夏回族自治区", "新疆维吾尔自治区"
        };
        while (it.hasNext()){
            String info = it.next();
            String[] split = info.split("-");
            //构造数据
            Person person = new Person();
            person.setName(split[0]);
            if(split[1].equals("男")){
                person.setSex(1);
            }
            person.setAge(Integer.parseInt(split[2]));

            Random random = new Random();
            int randomNumber = random.nextInt(28) + 1; // 生成1到10的随机数
            person.setNat(provinces[randomNumber]);
            list.add(person);
//            System.out.println(person);
        }
        Integer i = personMapper.insertUsers(list);
        return i;
    }

}

